package com.kingdom.admin.service;

import com.kingdom.admin.dao.SystemUserMapper;
import com.kingdom.admin.entity.SystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by xinchunting on 17-12-4.
 */
@Service
public class SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;


    public SystemUser insert(SystemUser systemUser) {
        Long time = System.currentTimeMillis();
        systemUser.setCtime(time);
        systemUser.setMtime(time);
        systemUserMapper.insert(systemUser);
        return systemUser;
    }

    public int delete(Long id) {
        return 0;
    }

    public SystemUser update(SystemUser systemUser) {
        systemUserMapper.updateByPK(systemUser);
        return systemUser;
    }

    public SystemUser selectByPK(Long id) {
        return systemUserMapper.selectByPK(id);
    }

    public SystemUser selectByUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        SystemUser systemUser = new SystemUser();
        systemUser.setUserName(username);
        List<SystemUser> users = systemUserMapper.select(systemUser);
        if (ObjectUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    public List<SystemUser> select(SystemUser systemUser) {
        return systemUserMapper.select(systemUser);
    }

}
